﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Threading;

namespace Sofire.DataComm
{

    /// <summary>
    /// 一个简单的邮件发送。
    /// </summary>
    public class SimpleMail
    {
        /// <summary>
        /// 系统邮件。发件人。
        /// </summary>
        public string ServiceMail { get; set; }

        /// <summary>
        /// 系统名称。发件人姓名。
        /// </summary>
        public string ServiceName { get; set; }

        /// <summary>
        /// 系统邮件的 SMTP 地址。
        /// </summary>
        public string ServiceHost { get; set; }

        /// <summary>
        /// 邮箱的账户。
        /// </summary>
        public string User { get; set; }

        /// <summary>
        /// 邮箱的密码。
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 初始化 <see cref="Sofire.DataComm.SimpleMail"/> 的新实例。
        /// </summary>
        /// <param name="serviceMail">系统邮件。发件人。</param>
        /// <param name="serviceName">系统名称。发件人姓名。</param>
        /// <param name="serviceHost">系统邮件的 SMTP 地址。</param>
        /// <param name="user">邮箱的账户。</param>
        /// <param name="pwd">邮箱的密码。</param>
        public SimpleMail(string serviceMail, string serviceName, string serviceHost, string user, string pwd)
        {
            this.ServiceMail = serviceMail;
            this.ServiceName = serviceName;
            this.ServiceHost = serviceHost;
            this.User = user;
            this.Password = pwd;

        }

        /// <summary>
        /// 发送邮件。
        /// </summary>
        /// <param name="subject">标题。</param>
        /// <param name="body">内容。</param>
        /// <param name="mailTo">收信人列表。</param>
        public void Send(string subject, string body, params string[] mailTo)
        {
            MailAddress[] address = new MailAddress[mailTo.Length];
            for(int i = 0 ; i < mailTo.Length ; i++)
            {
                address[i] = new MailAddress(mailTo[i]);
            }
            this.Send(subject, body, false, address);
        }

        /// <summary>
        /// 发送邮件。
        /// </summary>
        /// <param name="subject">标题。</param>
        /// <param name="body">内容。</param>
        /// <param name="mailTo">收信人列表。</param>
        public void Send(string subject, string body, params MailAddress[] mailTo)
        {
            this.Send(subject, body, false, mailTo);
        }

        /// <summary>
        /// 引发异常的处理委托。
        /// </summary>
        public event ExceptionEventHandler Exception;

        /// <summary>
        /// 发送邮件。
        /// </summary>
        /// <param name="subject">标题。</param>
        /// <param name="body">内容。</param>
        /// <param name="isBodyHtml">内容是否为 html 格式。</param>
        /// <param name="mailTo">收信人列表。</param>
        public void Send(string subject, string body, bool isBodyHtml, params MailAddress[] mailTo)
        {
            if(mailTo == null || mailTo.Length == 0) return;
            MailMessage _message = new MailMessage();
            _message.From = new MailAddress(this.ServiceMail, this.ServiceName);
            _message.Subject = subject;
            _message.Body = body;
            _message.IsBodyHtml = isBodyHtml;
            foreach(MailAddress item in mailTo)
                _message.To.Add(item);
            string[] hostAndPort = this.ServiceHost.Split(':');
            SmtpClient _smtpClient = new SmtpClient(hostAndPort[0]);
            if(hostAndPort.Length == 2) _smtpClient.Port = Convert.ToInt32(hostAndPort[1]);

            _smtpClient.Credentials = new NetworkCredential(this.User, this.Password);


            Thread t = new Thread(new ThreadStart(delegate()
                {
                    try
                    {
                        _smtpClient.Send(_message);
                    }
                    catch(Exception ex)
                    {
                        if(this.Exception != null) this.Exception(this, new ExceptionEventArgs(ex));
                        else throw ex;
                    }

                }));
            t.Start();
        }
    }
}
